package com.cn;

import com.topboya.officeuser.entity.Employee;
import com.topboya.officeuser.entity.User;
import com.topboya.officeuser.utils.Result;
import io.swagger.models.auth.In;
import org.junit.Test;

import java.security.PublicKey;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @className MutipartStreamMethod
 * @Author xiaxin
 * @Date 2020/8/25 16:53
 * @Version 1.0
 **/
public class MutipartStreamMethod {

    @Test
    public void test01(){
        /**
         * 创建流的方式1
         */
        List<String> list = new ArrayList<>();
        Stream<String> stream1 = list.stream();
        /**
         * 创建流的方式2
         */
        String[] strings = new String[10];
        Stream<String> stream2 = Arrays.stream(strings);
        /**
         * 创建流的方式3
         */
        Stream<Integer> stream3 = Stream.of(1,2,3);
        /**
         * 创建流的方式4
         */
        Stream<Integer> stream4 = Stream.iterate(0,(i)->++i+i++);
        stream4.forEach(System.out::println);

        //生成
        Stream.generate(()->Math.random())
                .limit(5)
                .forEach(System.out::println);
    }


    //String id, String loginName, String name, String email
    List<User> emps = Arrays.asList(
            new User("32","小李","李明","873735398@qq.com"),
            new User("332","小找","赵璐","873735392@qq.com"),
            new User("3332","萌娃","萌可","873735398@qq.com"),
            new User("33332","小雅","胡雅","873735395@qq.com"),
            new User("333332","小芝","兰芝","873735398@qq.com"),
            new User("33332","小雅","胡雅","873735398@qq.com"),
            new User("333332","小芝","兰芝","873735398@qq.com")
    );

    //筛选和切片
    @Test
    public void test003(){
        //emps.stream().filter((x)->x.getName().equals("萌可")).forEach(System.out::println);
        emps.stream().filter((x)->x.getEmail().equals("873735398@qq.com")).distinct().skip(1).limit(2).forEach(System.out::println);

    }
    //映射
    @Test
    public void test05(){
        List<String> list = Arrays.asList("a","b","c");
        list.stream().map(str->{return str.toUpperCase();}).forEach(System.out::println);
    }

    @Test
    public void test06(){
        emps.stream().map(u->u.getName()).forEach(System.out::println);
    }


    public Stream<Character> filterCharacter(String str){
        List<Character> list = new ArrayList<>();
        for (char c : str.toCharArray()){
            list.add(c);
        }
        return list.stream();
    }

    @Test
    public void test09(){
        List<String> list = Arrays.asList("a","b","c","d","e");
        MutipartStreamMethod md = new MutipartStreamMethod();
        list.stream()
                .flatMap(md::filterCharacter)
                .forEach(System.out::println);
    }

    @Test
    public void test10(){
        emps.stream().distinct().forEach(System.out::println);
    }

    @Test
    public void test11(){
        emps.stream().sorted((e1,e2)->{
            if(e1.getId().equals(e2.getId())){
                return e1.getId().compareTo(e2.getId());
            }else{
                return -e1.getId().compareTo(e2.getId());
            }
        }).forEach(System.out::println);
    }
    public enum Status{
        FREE,BUSY,VOCATION;
    }


    @Test
    public void test12(){
        List<Status> list = Arrays.asList(Status.BUSY,Status.FREE,Status.VOCATION);

        boolean flag1 = list.stream()
                .allMatch(s->s.equals(Status.FREE));
        System.out.println(flag1);

        boolean flag2 = list.stream()
                .anyMatch(s->s.equals(Status.BUSY));
        System.out.println(flag2);

        boolean flag3 = list.stream()
                .noneMatch(s->s.equals("ss"));
        System.out.println(flag3);

        Optional<Status> first = list.stream().findFirst();
        Status status = first.orElse(Status.FREE);
        System.out.println(status);

        Optional<Status> status1 = list.stream().findAny();
        System.out.println(status1.get());

        long count = list.stream().count();
        System.out.println(count);

    }

    @Test
    public void test13(){
        List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9);
        Integer integer = list.stream().reduce(0,(x,y)->x+y);
        System.out.println(integer);
    }

    @Test
    public void test14(){
        List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9);
        Integer integer = list.stream().reduce(0,(x,y)->x+y);
        System.out.println(integer);
    }

    @Test
    public void test15(){
        List<String> list = emps.stream()
                .map(User::getName)
                .distinct()
                .collect(Collectors.toList());
        list.forEach(System.out::println);
    }

    @Test
    public void test16(){
        Set<String> collectSet = emps.stream()
                .map(User::getName)
                .collect(Collectors.toSet());
        collectSet.forEach(System.out::println);
    }

    @Test
    public void test17(){
        Set<String> collectSet = emps.stream()
                .map(User::getName)
                .collect(Collectors.toSet());
        collectSet.forEach(System.out::println);
    }

    //放入LinkedHashSet
    @Test
    public void test18() {
        LinkedHashSet<String> linkedHashSet = emps.stream()
                .map(User::getEmail)
                .collect(Collectors.toCollection(LinkedHashSet::new));
        linkedHashSet.forEach(System.out::println);
    }

    @Test
    public void test19(){
        Long count = emps.stream()
                .collect(Collectors.counting());
        System.out.println(count);
        Double avg = emps.stream()
                .collect(Collectors.averagingDouble(
                        u->Double.parseDouble(u.getId())));
        System.out.println(avg);

        Double sum = emps.stream().collect(Collectors.summingDouble(u->Double.parseDouble(u.getId())));
        System.out.println(sum);

        Optional<User> collect = emps.stream()
                .collect(
                        Collectors.maxBy((e1, e2) -> Double.compare(Double.parseDouble(e1.getId()), Double.parseDouble(e2.getId()))));
        System.out.println(collect.get());

    }

    @Test
    public void test20(){
        Map<String, List<User>> collect = emps.stream().collect(Collectors.groupingBy(User::getName));
        System.out.println(collect);
    }

    @Test
    public void test21(){
        Map<String, List<User>> collect = emps.stream().collect(Collectors.groupingBy(User::getName));
        collect.forEach((key,value)->{
            System.out.println(key);
            System.out.println(value);
        });
        System.out.println(collect);
    }

    @Test
    public void test22(){
        Map<String, Map<String, List<User>>> collect = emps.stream().collect(Collectors.groupingBy(User::getName, Collectors.groupingBy(User::getId)));
        collect.forEach((u,i)->{
            i.forEach((v,k)->{
                System.out.println(v);
                System.out.println(k);
            });
            System.out.println(u);
        });
    }

    @Test
    public void test23(){
        Map<Boolean,List<User>> listMap = emps.stream()
                .collect(Collectors.partitioningBy(e->Integer.parseInt(e.getId())>5000));
        System.out.println(listMap);
    }

    @Test
    public void test24(){
        DoubleSummaryStatistics dss = emps.stream()
                .collect(Collectors.summarizingDouble(u -> Double.parseDouble(u.getId())));
        System.out.println(dss.getMax());
        System.out.println(dss.getMin());
        System.out.println(dss.getSum());
        System.out.println(dss.getCount());
        System.out.println(dss.getAverage());

        String str = emps.stream().map(User::getName).collect(Collectors.joining("_"));
        System.out.println(str);
    }

    @Test
    public void test25(){
        List<Integer> list = Arrays.asList(1,2,3,4,5);
        list.stream()
                .map((x)->x*x)
                .forEach(System.out::println);
    }

    @Test
    public void test26(){

        Optional<Integer> reduce = emps.stream()
                .map(e->1)                             //将每个元素映射成1；
                .reduce(Integer::sum);

        System.out.println(reduce.get());
    }

    @Test
    public void test27(){

        List<Integer> list = Arrays.asList(1,2,3,4,5,6,7,8,9);
        Integer integer = list.stream().reduce(3,(x,y)->x+y);

        System.out.println(integer);
    }
    List<Employee> ems = Arrays.asList(
            new Employee(101, "Z3", 19, 9999.99),
            new Employee(102, "L4", 20, 7777.77),
            new Employee(103, "W5", 35, 6666.66),
            new Employee(104, "Tom", 44, 1111.11),
            new Employee(105, "Jerry", 60, 4444.44)
    );

    @Test
    public void test28(){
        List<String> list = ems.stream()
                            .map(Employee::getName)
                            .collect(Collectors.toList());
        list.forEach(System.out::println);
    }

    @Test
    public void test29(){
        Set<String> set = ems.stream()
                .map(Employee::getName)
                .collect(Collectors.toSet());
        set.forEach(System.out::println);
    }

    @Test
    public void test30(){
        Set<String> set = ems.stream()
                .map(Employee::getName)
                .collect(Collectors.toSet());
        set.forEach(System.out::println);
    }

    @Test
    public void test31(){
        ArrayList<String> arrayListHa = ems.stream().map(Employee::getName)
                .collect(Collectors.toCollection(ArrayList::new));
        arrayListHa.forEach(System.out::println);

    }

    @Test
    public void test32(){
        //1总数
        Long count = ems.stream()
                     .collect(Collectors.counting());
        System.out.println(count);

        //2平均值
        DoubleSummaryStatistics collect2 = ems.stream()
                .collect(Collectors.summarizingDouble(Employee::getSalary));
        System.out.println(collect2.getAverage());

        //3最小薪资
        Optional<Double> collect = ems.stream()
                .map(Employee::getSalary)
                .collect(Collectors.minBy(Double::compareTo));
        System.out.println(collect.get());

        //4总和
        Double sums = ems.stream()
                .collect(Collectors.summingDouble(Employee::getSalary));
        System.out.println(sums);

        //5最大值
        Optional<Double> collect1 = ems.stream()
                .map(Employee::getSalary)
                .collect(Collectors.maxBy(Double::compareTo));
        System.out.println(collect1.get());
    }

    @Test
    public void test33(){
        Map<Integer,List<Employee>> map = ems.stream()
                .collect(Collectors.groupingBy(Employee::getId));

        Map<Integer, Map<String, List<Employee>>> binaryGroup = ems.stream()
                .collect(Collectors.groupingBy(Employee::getId, Collectors.groupingBy(Employee::getName)));
        System.out.println(map);

        binaryGroup.forEach((t,u)->{
            u.forEach((k,j)->{
                System.out.println(k);
                System.out.println(j);
            });
            System.out.println(t);
        });
    }

    @Test
    public void test34(){
        String str = emps.stream()
                .map(u->u.getName())
                .collect(Collectors.joining("-")); //可传入分隔符
        System.out.println(str);
    }




}
